package com.changgou.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    private static final String CART="cart_";
    //添加购物车
    @Override
    public void addCart(String skuId, Integer num, String username) {
        //查询redis中的商品信息
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART + username).get(skuId);
        //如果redis中存在该商品的信息,则更新数量和价钱
        if(orderItem!=null){
            orderItem.setNum(orderItem.getNum()+num);
            orderItem.setMoney(orderItem.getNum()*orderItem.getPrice());
            orderItem.setPayMoney(orderItem.getNum()*orderItem.getPrice());
        }else {
//            Sku sku = (Sku) skuFeign.findById(skuId).getData();
            String skuStr = JSON.toJSONString(skuFeign.findById(skuId).getData());
            Sku sku = JSON.parseObject(skuStr, Sku.class);
//            Spu spu = spuFeign.findSpuById(sku.getSpuId()).getData();
            String spuStr = JSON.toJSONString(spuFeign.findSpuById(sku.getSpuId()).getData());
            Spu spu = JSON.parseObject(spuStr, Spu.class);
            //封装orderItem
             orderItem = this.sku2OrderItem(sku, spu, num);
        }
        //如果不存在的话则添加商品到redis中
        redisTemplate.boundHashOps(CART + username).put(skuId,orderItem);
    }

    //查询购物车数据
    @Override
    public Map list(String username) {
        HashMap<String, Object> map = new HashMap<>();
        List<OrderItem> orderItemList = redisTemplate.boundHashOps(CART + username).values();
        map.put("orderItemList",orderItemList);

        //商品的总数量与总价格
        Integer totalNum=0;
        Integer totalMoney=0;

        for (OrderItem orderItem : orderItemList) {
            totalNum+=orderItem.getNum();
            totalMoney+=orderItem.getMoney();
        }
        map.put("totalNum",totalNum);
        map.put("totalMoney",totalMoney);
        return map;
    }

    private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {
        OrderItem orderItem = new OrderItem();
        orderItem.setSpuId(spu.getId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(orderItem.getPrice()*num);
        orderItem.setPayMoney(orderItem.getPrice()*num);
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight()*num);
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());
        return orderItem;
    }
}
